package com.xiyi.shouxin.controller;

import java.util.Calendar;
import java.util.List;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xiyi.shouxin.controller.pojo.StatisticsView;
import com.xiyi.shouxin.controller.pojo.UserGroupRankParams;
import com.xiyi.shouxin.controller.pojo.UserStatisticsView;
import com.xiyi.shouxin.controller.pojo.YearUserParams;
import com.xiyi.shouxin.entity.admin.Admin;
import com.xiyi.shouxin.entity.user.User;
import com.xiyi.shouxin.init.CacheUtil;
import com.xiyi.shouxin.service.PracticeService;
import com.xiyi.shouxin.utils.ExceptionUtil;
import com.xiyi.shouxin.view.CommonModel;
import com.xiyi.shouxin.view.pojo.PracticeView;

@CrossOrigin("*")
@RequestMapping("/statistcs")
@RestController
public class StatistcsController {
    // private Gson gson = new Gson();
    @Autowired
    private Logger logger;
    @Autowired
    private CacheUtil cacheUtil;
    @Autowired
    private PracticeService practiceService;

    @PostMapping("/statistcs_practice")
    public CommonModel statistcsPractice() {
        Long adminId = null;
        Admin admin = cacheUtil.getAdminOperator();
        if (admin != null) {
            adminId = admin.getId();
        }
        try {
            StatisticsView view = practiceService.statisticsPractice(adminId);
            return CommonModel.Success(view);
        } catch (Exception e) {
            logger.error("查询统计数据异常：", e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    @PostMapping("/year_user")
    public CommonModel yearUser(@RequestBody YearUserParams params) {
        Long userId = null;
        User user = cacheUtil.getUserOperator();
        if (user != null) {
            userId = user.getId();
        }
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        if (params.getYear() != null) {
            year = params.getYear();
        }
        try {
            UserStatisticsView view = practiceService.statisticsUserYearData(userId, year);
            return CommonModel.Success(view);
        } catch (Exception e) {
            logger.error("查询统计数据异常：", e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    @PostMapping("/user_pass_rank")
    public CommonModel userPassRank(@RequestBody UserGroupRankParams params) {
        Long adminId = params.getAdminId();
        Admin admin = cacheUtil.getAdminOperator();
        if (params.getAdminId() == null) {
            adminId = admin.getId();
        }
        try {
            List<PracticeView> view = practiceService.userGroupDailyPassRateRank(adminId, params.getDate());
            return CommonModel.Success(view);
        } catch (Exception e) {
            logger.error("查询统计数据异常：", e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    @PostMapping("/user_qualified_rank")
    public CommonModel userQualifiedRank(@RequestBody UserGroupRankParams params) {
        Long adminId = params.getAdminId();
        Admin admin = cacheUtil.getAdminOperator();
        if (params.getAdminId() == null) {
            adminId = admin.getId();
        }
        try {
            List<PracticeView> view = practiceService.userGroupDailyQualifiedRank(adminId, params.getDate());
            return CommonModel.Success(view);
        } catch (Exception e) {
            logger.error("查询统计数据异常：", e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }
}
